<?php
/**
 * ============================================================================
 * Author: Abing
 * Date: 17-3-21
 * 联系QQ:501443501
 * ============================================================================
 */
require_once __DIR__.'/errorCode.php';
class User{
    private $_db;
    /**
     * 构造方法
     * user constructor
     * @param PDO $_db PDO连接句柄
     */
    public function __construct($_db){
        $this->_db = $_db;
    }

    /**
     * 用户登录
     * @param $username
     * @param $password
     */
    public function login($username,$password){
        if(empty($username)){
            throw new Exception('用户名不能为空',ErrorCode::USERNAME_EMPTY);
        }
        if(empty($password)){
            throw new Exception('密码不能为空',ErrorCode::PASSWORD_EMPTY);
        }
        $sql = 'SELECT * FROM `user` WHERE `username`=:username AND `password`=:password';
        $stmt = $this->_db->prepare($sql);
        $password = $this->_md5($password);
        $stmt->bindParam(':username',$username);
        $stmt->bindParam(':password',$password);
        $stmt->execute();
        $user = $stmt->fetch(PDO::FETCH_ASSOC);
        if(empty($user)){
            throw new Exception('用户名或密码错误',ErrorCode::USERNAME_ORPASSWORD_WORN);
        }
            unset($user['password']);
            return $user;
    }

    /**
     * 用户注册
     * @param $username
     * @param $password
     */
    public function register($username,$password){
        if(empty($username)){
            throw new Exception('用户名不能为空',ErrorCode::USERNAME_EMPTY);
        }
        if(empty($password)){
            throw new Exception('密码不能为空',ErrorCode::PASSWORD_EMPTY);
        }
        if($this->_isUsernamneExists($username)){
            throw new Exception('用户名已存在',ErrorCode::USERNAME_EXISTS);
        }
        //写入数据库
        $sql = 'INSERT INTO `user`(`username`,`password`,`create_time`) VALUES (:username,:password,:create_time)';
        $create_time = time();
       // var_dump($create_time);
        //exit;
        $stmt = $this->_db->prepare($sql);
        $stmt->bindParam(':username',$username);
        $password = $this->_md5($password);
        $stmt->bindParam(':password',$password);
        $stmt->bindParam(':create_time',$create_time);
        if(!$stmt->execute()){
            throw new Exception('注册失败',ErrorCode::REGISTER_FAIL);
        }
        return [
            'user_id' => $this->_db->lastInsertId(),
            'username' => $username,
            'create_time' => $create_time
            ];
    }

    /**
     * MD5加密
     * @param $string
     * @param string $key
     * @return string
     */
    private function _md5($string,$key = 'hello'){
        return md5($string.$key);
    }

    /**
     * 检测用户名是否存在
     * @param $username
     * @return bool
     */
    private function _isUsernamneExists($username){
        $sql = 'SELECT * FROM `user` WHERE `username`=:username';
        $stmt = $this->_db->prepare($sql);
        $stmt->bindParam(':username',$username);
        $stmt->execute();
        $result = $stmt->fetch(PDO::FETCH_ASSOC);
        return !empty($result);
    }
}